﻿using System.Windows.Forms;
using BarCode.AccessDao;
using System.Collections.Generic;
using BarCode.Bean;
using BarCode.Common;
using System;
using System.Text;

namespace BarCode.Forms
{
    public partial class Login : Form
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(Login));

        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, System.EventArgs e)
        {
            List<WorkShopBean> listWorkShop = new List<WorkShopBean>();
            listWorkShop.Add(new WorkShopBean() { name = "注塑" });
            listWorkShop.Add(new WorkShopBean() { name = "机芯" });
            listWorkShop.Add(new WorkShopBean() { name = "整表" });
            cmbWorkShop.DataSource = listWorkShop;
            cmbWorkShop.DisplayMember = "name";
            cmbWorkShop.ValueMember = "name";
        }

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            // 退出系统
            Application.Exit();
        }

        private void cmbUser_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            txtPwd.Text = "";
        }

        private void cmbWorkShop_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (cmbWorkShop.SelectedValue.ToString() != null)
            {
                //txtPwd.Text = "";
                DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                List<UserBean> lstUser = new List<UserBean>();
                if (cmbWorkShop.SelectedValue.Equals("注塑") || cmbWorkShop.SelectedValue.Equals("机芯") || cmbWorkShop.SelectedValue.Equals("整表"))
                {
                    lstUser = db01.getUserInfo(cmbWorkShop.SelectedValue.ToString());
                }
                else
                {
                    WorkShopBean ws = (WorkShopBean)cmbWorkShop.SelectedValue;
                    lstUser = db01.getUserInfo(ws.name);
                }
                cmbUser.DataSource = lstUser;
                cmbUser.DisplayMember = "User";
                cmbUser.ValueMember = "User";
            }

        }

        public int getErrorInformation(int error_code, out string message)
        {
            int result = 0;
            byte[] errorInfo = new byte[1024];
            PrintLab.PTK_GetErrorInfo(error_code, errorInfo, errorInfo.Length);
            message = Encoding.Default.GetString(errorInfo).Trim('\0');
            return result;
        }


        private void showError(string FunctionName, int error_code)
        {
            PrintLab.PTK_CloseUSBPort();
            var message = "";
            int result = getErrorInformation(error_code, out message);
            MessageBox.Show(FunctionName + ":" + message);
        }

        private byte[] E_UTF8(string data)
        {
            return Encoding.UTF8.GetBytes(data);
        }

        private void btnOK_Click(object sender, System.EventArgs e)
        {
            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            UserBean loginUser = db01.getCmbWorkShopInfo(cmbWorkShop.SelectedValue.ToString(), cmbUser.SelectedValue.ToString());
            if ("".Equals(loginUser.Password) || loginUser.Password == null)
            {
                if (txtPwd.Text == "")
                {
                    GlobalVal.CurrentUser = loginUser;
                    JK_BarCode jkBarCode = new JK_BarCode();
                    jkBarCode.Show();
                }
                else
                {
                    MessageUtil.Warn("密码不正确，请重新输入！");
                    txtPwd.SelectAll();
                    txtPwd.Focus();
                    return;
                }
            }
            else
            {
                if (txtPwd.Text.Equals(loginUser.Password))
                {
                    GlobalVal.CurrentUser = loginUser;
                    JK_BarCode jkBarCode = new JK_BarCode();
                    jkBarCode.Show();
                }
                else
                {
                    MessageUtil.Warn("密码不正确，请重新输入！");
                    txtPwd.SelectAll();
                    txtPwd.Focus();
                    return;
                }
            }
            this.Hide();
        }

        private void label1_Click(object sender, System.EventArgs e)
        {

        }

        private void Login_Load_1(object sender, EventArgs e)
        {

        }
    }



}
